package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusProvider;
import com.example.cheeseerpadmin.bus.service.BusProviderService;
import com.example.cheeseerpadmin.bus.vo.ProviderVo;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-14
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    public BusProviderService busProviderService;

    /**
     * 加载全部数据并且做模糊查询
     *
     * @param providerVo
     * @return
     */
    @RequestMapping("/loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo providerVo) {
        Page<BusProvider> busProviderPage = new Page<>();
        QueryWrapper<BusProvider> busProviderQueryWrapper = new QueryWrapper<>();
        busProviderQueryWrapper.like(StringUtils.isNotBlank(providerVo.getProvidername()), "providername", providerVo.getProvidername());
        busProviderQueryWrapper.like(StringUtils.isNotBlank(providerVo.getTelephone()), "telephone", providerVo.getTelephone());
        busProviderQueryWrapper.like(StringUtils.isNotBlank(providerVo.getConnectionperson()), "connectionperson", providerVo.getConnectionperson());
        Page<BusProvider> page = busProviderService.page(busProviderPage, busProviderQueryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 增加客户
     *
     * @param busProvider
     * @return
     */
    @RequestMapping("/addProvider")
    public ResultObj addProvider(BusProvider busProvider) {
        try {
            busProviderService.save(busProvider);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新客户
     *
     * @param providerVo
     * @return
     */
    @RequestMapping("/updateProvider")
    public ResultObj updateProvider(ProviderVo providerVo) {
        try {
            busProviderService.updateById(providerVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 批量删除客户
     * @param providerVo
     * @return
     */
    @RequestMapping("/batchDeleteProvider")
    public ResultObj batchDeleteProvider(ProviderVo providerVo) {
        try {
            Integer[] ids = providerVo.getIds();
            for (Integer id : ids) {
                busProviderService.removeById(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @RequestMapping("/deleteProviderById")
    public ResultObj deleteProviderById(Integer id){
        try {
            busProviderService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

